package cn.tedu.tmall.admin.account.dao.persist.repository.impl;

import cn.tedu.tmall.admin.account.dao.persist.mapper.UserMapper;
import cn.tedu.tmall.admin.account.dao.persist.repository.IUserRepository;
import cn.tedu.tmall.admin.account.pojo.entity.User;
import cn.tedu.tmall.admin.account.pojo.vo.UserListItemVO;
import cn.tedu.tmall.admin.account.pojo.vo.UserStandardVO;
import cn.tedu.tmall.common.pojo.vo.PageData;
import cn.tedu.tmall.common.util.PageInfoToPageDataConverter;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Repository;

import java.util.List;

@Slf4j
@Repository
public class UserRepositoryImpl implements IUserRepository {
    @Autowired
    private UserMapper userMapper;
    @Override
    public int insert(User user) {
        log.debug("开始执行插入用户信息");
        return userMapper.insert(user);
    }

    @Override
    public int countByUsername(String username) {
        log.debug("开始执行根据用户名查询数据");
       /* QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("username",username);
        return userMapper.selectCount(queryWrapper);*/
        return userMapper.selectCount(Wrappers.<User>lambdaQuery().eq(User::getUsername, username));
    }

    @Override
    public int countByPhone(String phone) {
        log.debug("开始执行根据手机号查询数据");
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone", phone);
        return userMapper.selectCount(queryWrapper);
    }

    @Override
    public int countByEmail(String email) {
        log.debug("开始执行根据邮箱查询数据");
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("email", email);
        return userMapper.selectCount(queryWrapper);
    }

    @Override
    public int updateById(User user) {
        log.debug("开始处理修改用户密码");
        return userMapper.updateById(user);
    }

    @Override
    public int countByPhoneAndNotId(String phone, Long userId) {
        log.debug("开始查询手机号是否重复");
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone", phone);
        queryWrapper.ne("Id", userId);
        return userMapper.selectCount(queryWrapper);
    }

    @Override
    public int countByEmailAndNotId(Long userId, String email) {
        log.debug("开始查询邮箱是否重复");
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("email",email);
        queryWrapper.ne("id",userId);
        return userMapper.selectCount(queryWrapper);
    }

    @Override
    public PageData<UserListItemVO> list(Integer pageNum, Integer pageSize) {
        log.debug("开始执行查询用户列表");
        PageHelper.startPage(pageNum, pageSize);
        List<UserListItemVO> list= userMapper.list();
        PageInfo<UserListItemVO> pageInfo=new PageInfo<>(list);
        return PageInfoToPageDataConverter.convert(pageInfo);
    }

    @Override
    public UserStandardVO getStandarById(Long userId) {
        return userMapper.getStandardById(userId);
    }


}
